#!/bin/sh
. /etc/utils.sh

MODULE_PATH=/usr/lib/modules

start()
{
  # Set up NEXUS first since it applies PINMUX.
  echo "Starting nexus"
  [ -e ${MODULE_PATH}/nexus.ko ] && insmod ${MODULE_PATH}/nexus.ko
  if [ -e ${MODULE_PATH}/bcmdriver.ko ]; then
    insmod ${MODULE_PATH}/bcmdriver.ko
    mknod /dev/brcm0 c 30 0
    chmod 666 /dev/brcm0

    # Set up space for multiprocess NEXUS unix sockets
    mount -t tmpfs none /shared
  fi

  # Populate the hardware version
  HWVER=$(hnvram -qr HW_VER)
  # Chimera yellow board does not set HW_VER, it should be 1.
  [ "$PLATFORM" = "GFCH100" ] && [ -z "$HWVER" ] && HWVER=1

  [ -n "$HWVER" ] &&
  echo "$HWVER" >/etc/hw_ver &&
  chmod 444 /etc/hw_ver

  # Populate the serial number file.
  SERIAL=$(hnvram -br 1st_serial_number)
  [ -n "$SERIAL" ] &&
  echo "$SERIAL" >/etc/serial &&
  chmod 444 /etc/serial

  # Use the serial number to generate hostid
  serialmd5=$(md5sum /etc/serial)
  python -Sc "
import sys
hexstring = \"$serialmd5\"
sys.stdout.write(hexstring[:8].decode(\"hex\"))
  " > /etc/hostid
  chmod 444 /etc/hostid

  # Google Product Number
  GPN=$(hnvram -qr GPN)
  [ -n "$GPN" ] &&
  echo "$GPN" >/tmp/gpn &&
  chmod 444 /tmp/gpn

  # install driver modules needed early in order to setup GPIOs
  if is-ptp -a has_i2c_expander; then
    modprobe i2c_mux_pca954x
    modprobe gpio_pca953x

    echo pca9548 0x70 >/sys/bus/i2c/devices/i2c-0/new_device
    echo pca9546 0x70 >/sys/bus/i2c/devices/i2c-1/new_device
    echo tca6408 0x20 >/sys/bus/i2c/devices/i2c-9/new_device
    echo tca6408 0x20 >/sys/bus/i2c/devices/i2c-4/new_device
  fi

  # Do this before gpio-mailbox
  update-gpio-links

  # Start the gpio mailbox ASAP, since it blinks the LEDs to show boot progress.
  echo "Starting gpio-mailbox"
  mkdir -p /tmp/gpio/ledcontrol
  leds 4 0  # blink activity while booting
  echo 30 >/tmp/gpio/fanpercent  # low fan at first
  # is-secure-boot is ran after nexus on TV box
  if is-tv-box || is-secure-boot; then
    : >/tmp/gpio/ledcontrol/secure_boot
  else
    rm -f /tmp/gpio/ledcontrol/secure_boot
  fi
  ulimit -c 49152
  if runnable gpio-mailbox; then
    reboot-if-fail gpio-mailbox 2>&1 | logos gpio-mailbox &
  else
    # gpio-mailbox unavailable, but pretend it is so apps won't wait for it
    : >/var/run/gpio-mailbox
  fi

  if is-storage-box; then
    hostname GFiberStorage
  elif is-tv-box; then
    hostname GFiberTV
    if is-android-box; then
      setprop net.hostname GFiberTV
    fi
  elif is-network-box; then
    hostname GFiberRouter
  elif is-ptp; then
    hostname GFiberPTP
  else
    hostname GFiberUnknown
  fi

  # Don't let SCHED_FIFO / SCHED_RR realtime threads get preempted
  echo -1 >/proc/sys/kernel/sched_rt_runtime_us

  # populate hdcp key
  echo "Getting hdcp key"
  hnvram -br HDCP_KEY >/tmp/hdcp_key.tmp
  [ -s /tmp/hdcp_key.tmp ] && mv /tmp/hdcp_key.tmp /tmp/hdcp_key

  if startswith "$PLATFORM" GFRG2; then
    # Mindspeed-based devices can't fall back to GFP_DMA because they
    # use the GFP_DMA area for CONFIG_COMCERTO_ZONE_DMA_NCNB.  Thus, we
    # should preserve more space in main memory to ensure memory is
    # available on short notice for GFP_ATOMIC.
    echo 32768 >/proc/sys/vm/min_free_kbytes
  fi
}

stop()
{
  echo "stopping gpio-mailbox forcefully..."
  pkill -9 -f gpio-mailbox
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac
